package com.ktgj.customer.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.vo.GroupNumVo;
import com.ktgj.customer.domain.vo.SetOffTimeVo;
import com.ktgj.customer.mapper.*;
import com.ktgj.customer.service.ILoginService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.service.ITravelGroupNumberService;

import javax.servlet.http.HttpServletRequest;

/**
 * 旅游团号Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@Service
public class TravelGroupNumberServiceImpl implements ITravelGroupNumberService 
{
    @Autowired
    private TravelGroupNumberMapper travelGroupNumberMapper;

    @Autowired
    private TravelSupplierServiceImpl travelSupplierService;

    @Autowired
    private TravelContractServiceImpl travelContractService;

    @Autowired
    private TravelGroupMapper travelGroupMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderTouristMapper orderTouristMapper;

    @Autowired
    private ILoginService loginService;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 查询旅游团号
     * 
     * @param numberId 旅游团号ID
     * @return 旅游团号
     */
    @Override
    public TravelGroupNumberDto selectTravelGroupNumberById(Long numberId)
    {
        return travelGroupNumberMapper.selectTravelGroupNumberById(numberId);
    }

    /**
     * 查询旅游团号列表
     * 
     * @param travelGroupNumberDto 旅游团号
     * @return 旅游团号
     */
    @Override
    public List<TravelGroupNumberDto> selectTravelGroupNumberList(TravelGroupNumberDto travelGroupNumberDto)
    {
        return travelGroupNumberMapper.selectTravelGroupNumberList(travelGroupNumberDto);
    }

    /**
     * 新增旅游团号
     * 
     * @param travelGroupNumberDto 旅游团号
     * @return 结果
     */
    @Override
    public int insertTravelGroupNumber(TravelGroupNumberDto travelGroupNumberDto)
    {
        travelGroupNumberDto.setCreateTime(DateUtils.getNowDate());
        return travelGroupNumberMapper.insertTravelGroupNumber(travelGroupNumberDto);
    }

    /**
     * 修改旅游团号
     * 
     * @param travelGroupNumberDto 旅游团号
     * @return 结果
     */
    @Override
    public int updateTravelGroupNumber(TravelGroupNumberDto travelGroupNumberDto)
    {
        return travelGroupNumberMapper.updateTravelGroupNumber(travelGroupNumberDto);
    }

    /**
     * 批量删除旅游团号
     * 
     * @param numberIds 需要删除的旅游团号ID
     * @return 结果
     */
    @Override
    public int deleteTravelGroupNumberByIds(Long[] numberIds)
    {
        return travelGroupNumberMapper.deleteTravelGroupNumberByIds(numberIds);
    }

    /**
     * 删除旅游团号信息
     * 
     * @param numberId 旅游团号ID
     * @return 结果
     */
    @Override
    public int deleteTravelGroupNumberById(Long numberId)
    {
        return travelGroupNumberMapper.deleteTravelGroupNumberById(numberId);
    }

    /**
     * 查询出发时间详情
     * */
    @Override
    public List<SetOffTimeVo> setOffTimeVoList(Long groupId) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<SetOffTimeVo> setOffTimeVoList = new ArrayList<>();
        List<TravelGroupNumberDto> travelGroupNumberDtos = travelGroupNumberMapper.selectTravelGroupNumberDto(groupId);
        for (TravelGroupNumberDto travelGroupNumberDto : travelGroupNumberDtos) {
            SetOffTimeVo setOffTimeVo = new SetOffTimeVo();
            setOffTimeVo.setSetOffDate(format.format(travelGroupNumberDto.getNumberDate()));
            setOffTimeVo.setPrice(travelGroupNumberDto.getPrice());

            TravelGroup travelGroupDto = travelGroupMapper.selectTravelGroupById(travelGroupNumberDto.getGroupId());
            List<OrderInfoDto> orderInfoDtos = orderInfoMapper.selectOrderInfoDtoByNumberId(travelGroupNumberDto.getNumberId());

            int tmpCount = 0;
            for (OrderInfoDto orderInfoDto : orderInfoDtos) {
                List<OrderTouristDto> orderTouristDtos = orderTouristMapper.selectOrderTouristDtoByOrderId(orderInfoDto.getOrderId());
                tmpCount = tmpCount + orderTouristDtos.size();
            }

            Long count = travelGroupDto.getAduCount() - tmpCount;

            setOffTimeVo.setRemainingPeople(count.toString());
            setOffTimeVoList.add(setOffTimeVo);
        }
        return setOffTimeVoList;
    }

    /**
     * 团号列表
     * */
    @Override
    public List<TravelGroupNumber> groupNumList(GroupNumVo groupNumVo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<Long> idList = new ArrayList<>();
        if (groupNumVo.getCountryType() != null){
            List<TravelGroup> travelGroups = travelGroupMapper.travelGroupList(groupNumVo.getCountryType());
            if (travelGroups != null && travelGroups.size() > 0){
                for (TravelGroup travelGroup : travelGroups) {
                    idList.add(travelGroup.getGroupId());
                }
            }
        }

        groupNumVo.setIdList(idList);

        List<TravelGroupNumber> travelGroupNumberList = travelGroupNumberMapper.groupNumList(groupNumVo);

        if (travelGroupNumberList != null && travelGroupNumberList.size() > 0){
            for (TravelGroupNumber travelGroupNumber : travelGroupNumberList) {
                TravelGroup travelGroupDto = travelGroupMapper.selectTravelGroupById(travelGroupNumber.getGroupId());
                if (travelGroupDto.getCountryType() != null){
                    if (travelGroupDto.getCountryType().equals("1")){
                        travelGroupNumber.setCountryTypeName("国内团");
                    }
                    else if (travelGroupDto.getCountryType().equals("2")){
                        travelGroupNumber.setCountryTypeName("国际团");
                    }
                }

                if (travelGroupNumber.getStartTime() != null && travelGroupNumber.getEndTime() != null){
                    String startTime = format.format(travelGroupNumber.getStartTime());
                    String endTime = format.format(travelGroupNumber.getEndTime());
                    travelGroupNumber.setRoundTrip(startTime + "至" + endTime);
                }

                if (travelGroupNumber.getNumberStatus() != null){
                    if (travelGroupNumber.getNumberStatus().equals("0")){
                        travelGroupNumber.setNumberStatusName("待上架");
                    }
                    else if (travelGroupNumber.getNumberStatus().equals("1")){
                        travelGroupNumber.setNumberStatusName("已上架");
                    }
                    else if (travelGroupNumber.getNumberStatus().equals("2")){
                        travelGroupNumber.setNumberStatusName("已下架");
                    }
                }

                List<OrderInfoDto> orderInfoDtos = orderInfoMapper.selectOrderInfoDtoByNumberId(travelGroupNumber.getNumberId());
                if (orderInfoDtos != null){
                    travelGroupNumber.setOrderCount(orderInfoDtos.size());

                    int count = 0;
                    if (orderInfoDtos.size() > 0){
                        for (OrderInfoDto orderInfoDto : orderInfoDtos) {
                            List<OrderTouristDto> orderTouristDtos = orderTouristMapper.selectOrderTouristDtoByOrderId(orderInfoDto.getOrderId());
                            if (orderTouristDtos != null && orderTouristDtos.size() > 0){
                                count = count + orderTouristDtos.size();
                            }
                        }
                        travelGroupNumber.setApplicantsNum(count);
                    }
                }

                int remainingPlaces = 0;
                if (travelGroupDto.getAduCount() != null && travelGroupDto.getAduCount() != 0){
                    remainingPlaces = Integer.parseInt(travelGroupDto.getAduCount().toString()) - travelGroupNumber.getApplicantsNum();
                }
                travelGroupNumber.setRemainingPlaces(remainingPlaces);
            }
        }
        return travelGroupNumberList;
    }

    /**
     * 新增团号
     * */
    @Override
    public int insertGroupNum(HttpServletRequest request, TravelGroupNumber travelGroupNumber) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        travelGroupNumber.setGroupNumber("JS" + format.format(date));
        travelGroupNumber.setCreateTime(date);

        Employee loginEmployee = loginService.getLoginEmployee(request);
        if (loginEmployee == null){
            throw new Exception("登录已过期");
        }
        travelGroupNumber.setEmployeeId(loginEmployee.getEmployeeId());

        if (travelGroupNumber.getStartDate() != null && !travelGroupNumber.getStartDate().equals("")){
            travelGroupNumber.setStartTime(dateFormat.parse(travelGroupNumber.getStartDate()));
        }

        if (travelGroupNumber.getEndDate() != null && !travelGroupNumber.getEndDate().equals("")){
            travelGroupNumber.setEndTime(dateFormat.parse(travelGroupNumber.getEndDate()));
        }

        if (travelGroupNumber.getNumberTime() != null && !travelGroupNumber.getNumberTime().equals("")){
            try {
                travelGroupNumber.setNumberDate(dateFormat.parse(travelGroupNumber.getNumberTime()));
            } catch (Exception e){
                throw new Exception("日期格式错误");
            }
        }
        if (travelGroupNumber.getBackTime() != null && !travelGroupNumber.getBackTime().equals("")){
            try {
                travelGroupNumber.setBackDate(dateFormat.parse(travelGroupNumber.getBackTime()));
            } catch (Exception e){
                throw new Exception("日期格式错误");
            }
        }
        int i = travelGroupNumberMapper.insertGroupNum(travelGroupNumber);
        return i;
    }

    /**
     * 查询待修改的团号
     * */
    @Override
    public TravelGroupNumber selGroupNum(Long id) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TravelGroupNumber travelGroupNumber = travelGroupNumberMapper.selGroupNum(id);
        if (travelGroupNumber.getStartTime() != null){
            travelGroupNumber.setStartDate(format.format(travelGroupNumber.getStartTime()));
        }
        if (travelGroupNumber.getEndTime() != null){
            travelGroupNumber.setEndDate(format.format(travelGroupNumber.getEndTime()));
        }
        if (travelGroupNumber.getNumberDate() != null){
            travelGroupNumber.setNumberTime(format.format(travelGroupNumber.getNumberDate()));
        }
        if (travelGroupNumber.getSupplierId() != null){
            TravelSupplierDto travelSupplierDto = travelSupplierService.selectTravelSupplierById(travelGroupNumber.getSupplierId());
            if (travelSupplierDto != null){
                travelGroupNumber.setSupplierName(travelSupplierDto.getSupplierName());
            }
        }
        if (travelGroupNumber.getContractId() != null){
            TravelContractDto travelContractDto = travelContractService.selectTravelContractById(travelGroupNumber.getContractId());
            if (travelContractDto != null){
                travelGroupNumber.setContractName(travelContractDto.getContractNum());
            }
        }
        if (travelGroupNumber.getEmployeeId() != null){
            Employee employee = employeeMapper.backSelectEmployee(travelGroupNumber.getEmployeeId());
            if (employee != null){
                travelGroupNumber.setEmployeeName(employee.getEmployeeName());
            }
        }
        return travelGroupNumber;
    }

    /**
     * 修改团号
     * */
    @Override
    public int updateNum(HttpServletRequest request, TravelGroupNumber travelGroupNumber) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Employee loginEmployee = loginService.getLoginEmployee(request);
        if (loginEmployee == null){
            throw new Exception("登录已过期");
        }
        travelGroupNumber.setEmployeeId(loginEmployee.getEmployeeId());

        if (travelGroupNumber.getStartDate() != null && !travelGroupNumber.getStartDate().equals("")){
            travelGroupNumber.setStartTime(dateFormat.parse(travelGroupNumber.getStartDate()));
        }

        if (travelGroupNumber.getEndDate() != null && !travelGroupNumber.getEndDate().equals("")){
            travelGroupNumber.setEndTime(dateFormat.parse(travelGroupNumber.getEndDate()));
        }

        if (travelGroupNumber.getNumberTime() != null && !travelGroupNumber.getNumberTime().equals("")){
            try {
                travelGroupNumber.setNumberDate(dateFormat.parse(travelGroupNumber.getNumberTime()));
            } catch (Exception e){
                throw new Exception("日期格式错误");
            }
        }
        int i = travelGroupNumberMapper.updateNum(travelGroupNumber);
        return i;
    }

    /**
     * 团号上下架操作
     * */
    @Override
    public int operate(Long type, Long id) {
        int operate = travelGroupNumberMapper.operate(type, id);
        return operate;
    }
}
